package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    /* public Result upload(String username, Integer age, MultipartFile file) throws Exception {
         log.info("上传文件: {},{},{}", username, age, file );
         String filename =file.getOriginalFilename();
         String extname = filename.substring(filename.lastIndexOf("."));
         String newFileName = UUID.randomUUID().toString() + extname;
         file.transferTo(new File("E:/images/"+ newFileName));
         return Result.success();
     }*/
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @Log
    @PostMapping("/upload")
    public Result uploat(MultipartFile image) throws Exception {
        String filename = image.getOriginalFilename();
        String extname = filename.substring(filename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + extname;
        String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(url);
    }
}

